define(function(){
    var labelSearch = {
        ulist:-1,
        init:function(){
            this.bindFun();
        },
        bindFun:function(){
            var searchSleepSecond = 1000,
                searchStatus = true,
                searchData = new Object(),
                that = this;

            $('#searchlabel').keydown(function(e){
                $('.searchinput').errorTips().remove();
                $(this).parents('dl').removeClass('error');
                if(e.keyCode == 38){
                    if(that.ulist > 0 && !$(".label-seach-result").is(":hidden")){
                        that.ulist--;
                        that.writeFun();
                    }
                    return false;
                }
                if(e.keyCode == 40 && !$(".label-seach-result").is(":hidden")){
                    if(that.ulist < $("#label-seach-result-ul li").length-1){
                        that.ulist++;
                        that.writeFun();
                    }
                    return false;
                }
            })

            $('#searchlabel').keyup(function(e){
                $('.searchinput').errorTips().remove();
                $(this).parents('dl').removeClass('error');
                if(e.keyCode == 38 || e.keyCode == 40 ){
                    return false;
                }

                if(searchStatus && e.keyCode != 13){
                    searchStatus = false;
                    setTimeout(function(){
                        searchStatus = true;
                    } , this.searchSleepSecond);

                    var searchContent = $(this).val();
                    var searchUrl = 'http://www.shihuo.cn/article/getAutocompleteTags';

                    if(!searchContent){
                        $(".searchinput").removeClass("drop");
                        $(".label-seach-result").hide();
                        return false;
                    }

                    if(typeof searchData[searchContent] == 'undefined'){
                        searchContent = searchContent.replace(/\'/ig,"*");
                        $.post(searchUrl,{keyword:searchContent},function(msg){
                            if(msg.status){
                                searchData[searchContent] = msg.data;
                                var str='';
                                for(var i= 0; i < msg.info.length; i++){
                                    str += '<li class="label-overflow" data-key="'+msg.info[i].id+'">'+msg.info[i].name+'</li>';
                                }

                                $(".searchinput").addClass("drop");
                                $('.label-seach-result ul').html(str);
                                $(".label-seach-result").show();
                            }
                        },'json')
                    }else{
                        var str='';
                        for(var i= 0; i < searchData[searchContent].length; i++){
                            str += '<li class="label-overflow" data-key="'+searchData[searchContent][i]+'">'+searchData[searchContent][i]+'</li>';
                        }

                        if(str == ''){
                            $(".searchinput").removeClass("drop");
                            $(".label-seach-result").hide();
                        }else{
                            $(".searchinput").addClass("drop");
                            $(".label-seach-result").show();
                            $('.label-seach-result ul').html(str);
                        }
                    }

                    // console.log(searchData);
                }else{
                    $(this).attr("data-complete") == 1 && that.addLabel($(this).attr('data-key'),$(this).val());
                }
            });

            $("#label-seach-result-ul li").live('mousemove',function(data){
                $(this).addClass('on');
            });

            $("#label-seach-result-ul li").live('mouseout',function(data){
                $(this).removeClass('on');
            });

            $(".label .close").live('click',function(){
               $(this).parent().remove();
            });

            $(".label-overflow").live('click',function(){
                $("#searchlabel").val('');
                that.addLabel($(this).attr('data-key'),$(this).text());
            });

            $("#searchlabel").bind('input propertychange',function(){
                $(this).attr('data-complete','0');
            });
        },
        addLabel:function(datakey,text){
            var searcharr = [];
            var search = $('#searchlabel').val();
            for(var i=0;i<$('.label li').length;i++){
                searcharr.push($('.label li:eq('+i+')').find("span").text());
            }
            /*if(search == ''){
                $('#searchlabel').parents('dl').addClass('error');
                $('.searchinput').errorTips('请按回车添加').show();
            }else */
            if(searcharr.indexOf(text) < 0){
                $('.label').append('<li data-key='+datakey+'><span>'+text+'</span><i class="close">X</i></li>');
                $(".label-seach-result").hide();
            }
        },
        writeFun:function(){
            var $searchli = $("#label-seach-result-ul li");
            $searchli.removeClass('on');
            $searchli.eq(this.ulist).addClass('on');
            $("#searchlabel").val($searchli.eq(this.ulist).html()).attr({"data-complete":'1',"data-key":$searchli.eq(this.ulist).attr("data-key")});
        }
    }

    return labelSearch;
});